package cn.z2huo.knowledge.designpattern.proxy.dynamic.example3;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * <p>
 *
 * @author z2huo
 */
class MyInvocationHandler implements InvocationHandler {

    private final Object object;

    public MyInvocationHandler(Object object) {
        this.object = object;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("代理对象：" + proxy.getClass().getName());
        System.out.println(method.getName() + " before");
        Object object = method.invoke(this.object, args);
        System.out.println(method.getName() + " after");
        return object;
    }

    public static <T> T getProxy(Class<T>[] interfaceArray, T object) {
        return (T) Proxy.newProxyInstance(object.getClass().getClassLoader(), interfaceArray, new MyInvocationHandler(object));
    }

}
